import 'dart:async';

import 'package:binance_spot/binance_spot.dart';
import 'package:either_dart/src/either.dart';
import 'package:flutter/material.dart' hide Interval;
import 'package:flutter/cupertino.dart' hide Interval;


class GlobalParams{

  static List<double> buy20 =[];
  static List<double> sell20 =[];

  static Map<String, double> moneys= {'USDT':0,'BUSD':0,'BTC':0,'BNB':0};

  static String type = '';
  static String title = '';

  static int changwei = 10;

  static int pankou_yellow = 1500;
  static bool pankou_yellow_is = true;
  static int pankou_red= 2500;
  static bool pankou_red_is = true;
  static int pankou_blue = 3500;
  static bool pankou_blue_is = true;
  static int pankou_wihte = 5000;
  static bool pankou_wihte_is = true;

  static double form_yellow = 0.5;
  static bool form_yellow_is = true;
  static double form_red= 1.5;
  static bool form_red_is = true;
  static double form_blue = 2.5;
  static bool form_blue_is = true;
  static double form_wihte = 3.5;
  static bool form_wihte_is = true;
}

class Util{
  static Flex FlexContainer(Axis direction,List<Widget> children){
    return Flex(
        direction: direction,
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        verticalDirection: VerticalDirection.down,
        textDirection: TextDirection.rtl,
        children:children
    );
  }
  static Text TextCenter(String text, {Key? key}){
    return Text(
        text,
        textAlign: TextAlign.center,
        key: key);
  }

  static Text RedTextCenter(String text, {Key? key}){
    return Text(
        text,
        textAlign: TextAlign.center,
        style:const TextStyle(
          fontSize:15.0, // 文字大小
          color:Colors.red, // 文字颜色
        ),
        key: key);
  }

}


class API{
  BinanceSpot binanceSpot = BinanceSpot(
    key: "RCzHneTvJHxYMP4WvmJWXO7GMHNSMebuhWswiuLtYT8nFnaZ9sf4jBaH8rkO5ZaC",
    secret: "A8D92mZXRsAOKWS7QLKmxUddljAbnZV733DM6e22YOSirEQFdj03yYNt0gBdsKJ5",
  );
  late var  data;
  String key = "RCzHneTvJHxYMP4WvmJWXO7GMHNSMebuhWswiuLtYT8nFnaZ9sf4jBaH8rkO5ZaC";
  Future get(int num, bool buy_sell,bool fenchang){
    double formP = buy_sell ? GlobalParams.buy20[num] : GlobalParams.sell20[num];
    int index = formP.toString().substring(formP.toString().indexOf(".")).length-1;
    print(index);print(formP);
    double n = double.parse('0.'+('0'*(index-1))+'1');
    String ps;
    double new_ps;
    if(fenchang){
      ps =  ((GlobalParams.moneys[GlobalParams.type]! / GlobalParams.changwei)/formP).toStringAsFixed(index).toString();//数量
      String fps = ps.replaceRange(ps.length-1, ps.length, '0');
      new_ps = double.parse(fps);//数量
      formP = formP + n;

    }else{
      ps =  (GlobalParams.moneys[GlobalParams.type]! / formP).toStringAsFixed(index).toString();//数量
      String fps = ps.replaceRange(ps.length-1, ps.length, '0');
      new_ps = double.parse(fps);//数量
    }

    data = binanceSpot.newOrder(
        symbol: GlobalParams.title,
        side: Side.BUY,
        type: OrderType.LIMIT,
        quantity:"$new_ps",
        // quoteOrderQty:'0.01',
        price:'$formP',
        newClientOrderId:'self-1',
        // stopPrice:'1',
        icebergQty:"$new_ps",

        recvWindow:60000,
        timeInForce:  TimeInForce.GTC
    ).then((r){
      print('quantity:'+new_ps.toString()+' oldQ:'+ps+" formP:"+formP.toString()+" type:"+GlobalParams.type+" money"+GlobalParams.moneys[GlobalParams.type].toString());
      print(r.isLeft ? Left(r.left).value : '');
    });

    return data;
  }
  Future delete(int num, bool buy_sell,bool fenchang){
    double formP = buy_sell ? GlobalParams.buy20[num] : GlobalParams.sell20[num];
    String ps;
    double new_ps;
    if(fenchang){
      ps =  ((GlobalParams.moneys[GlobalParams.type]! / GlobalParams.changwei)/formP).toString();//数量
      String fps = ps.replaceRange(ps.length-1, ps.length, '0');
      new_ps = double.parse(fps);//数量
    }else{
      ps =  (GlobalParams.moneys[GlobalParams.type]! / formP).toString();//数量
      String fps = ps.replaceRange(ps.length-1, ps.length, '0');
      new_ps = double.parse(fps);//数量
    }

    data = binanceSpot.newOrder(
        symbol: GlobalParams.title,
        side: Side.SELL,
        type: OrderType.LIMIT,
        quantity:'11',  //交易数量
        // quoteOrderQty:'0.01',
        price:'1',
        newClientOrderId:'self-1',
        // stopPrice:'1',
        icebergQty:'10',
        recvWindow:60000,
        timeInForce:  TimeInForce.GTC
    );
    print(data.toString());
    return data;
  }
  List<Future<Either<String, List<FundingAsset>>>> getMoney(){
    return [
      binanceSpot.fundingWallet(
        asset: 'USDT',
        needBtcValuation: true,
        recvWindow: 60000,
      ),
      binanceSpot.fundingWallet(
        asset: 'BUSD',
        needBtcValuation: true,
        recvWindow: 60000,
      ),
      binanceSpot.fundingWallet(
        asset: 'BCT',
        needBtcValuation: true,
        recvWindow: 60000,
      )
    ];
  }

  void orderBook(String symbol){
    binanceSpot.orderBook(
      symbol: symbol,
      limit: 100,
    ).then((val){
      if(val.isLeft){
        print(val.left);
      }else{
        print(val.right);
      }
    });
  }

  void cancelBook(String symbol){
    binanceSpot.cancelallOpenOrders(symbol: symbol);
  }

  void ticker(String symbol){
    binanceSpot.tickerStream(symbol: 'USDT');
    binanceSpot.tickerStream(symbol: 'BUSD');
    binanceSpot.tickerStream(symbol: 'BTC');
  }

  void agg(String symbol){
    binanceSpot.aggregatedTradeStream(symbol: symbol);

  }


}


//买
// {
//   symbol: BUSDUSDT,
//   orderId: 529104737,
//   orderListId: -1,
//   clientOrderId: self-1,
//   transactTime: 1655300238620,
//   price: 1.00000000,
//   origQty: 11.00000000,
//   executedQty: 0.00000000,
//   cummulativeQuoteQty: 0.00000000,
//   status: NEW,
//   timeInForce: GTC,
//   type: LIMIT,
//   side: BUY,
//   icebergQty: 10.00000000,
//   fills: [
//
//   ]
// }


// 卖
// {
//   symbol: BUSDUSDT,// 交易对
//   orderId: 529094673,// 系统的订单ID
//   orderListId: -1, // OCO订单ID，否则为 -1
//   clientOrderId: self-1,// 客户自己设置的ID
//   transactTime: 1655299543724,// 交易的时间戳
//   price: 1.00000000,// 订单价格
//   origQty: 11.00000000,// 用户设置的原始订单数量
//   executedQty: 11.00000000,// 交易的订单数量
//   cummulativeQuoteQty: 11.01650000,// 累计交易的金额
//   status: FILLED,// 订单状态
//   timeInForce: GTC,// 订单的时效方式
//   type: LIMIT,// 订单的时效方式
//   side: SELL,// 订单方向，买还是卖
//   icebergQty: 10.00000000,
//   fills: [// 订单中交易的信息
//           {
//             price: 1.00150000,// 交易的价格
//             qty: 11.00000000,// 交易的数量
//             commission: 0.00000000,// 手续费金额
//             commissionAsset: BNB,// 手续费的币种
//             tradeId: 303454634 // 交易ID
//           }
//         ]
// }
